package com.whq.thrift.api.common.redis;

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

public class RedisHelper {
    protected RedisTemplate<String, String> redisTemplate = null;
	
	private RedisSerializer<String> getRedisSerializer() {  
        return redisTemplate.getStringSerializer();  
    } 
	
	public boolean expire(final String k, final long validSeconds) {  
        if (k == null) {  
            return false;  
        }  
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
            public Boolean doInRedis(RedisConnection connection)  
                    throws DataAccessException {  
                RedisSerializer<String> serializer = getRedisSerializer();  
                byte[] key  = serializer.serialize(k);  
                return connection.expire(key, validSeconds);  
            }  
        });  
        return result;  
    }  
	
	public void del(String key) {  
        List<String> list = new ArrayList<String>();  
        list.add(key);  
        del(list);  
    }  
	
	
	public void del(List<String> keys) {  
        redisTemplate.delete(keys); 
    }  
	
	/**
	 * 设置redis的值，如果没有的添加；如果有的话会修改
	 * 注意：如果之前k设置了有效时间，调用该接口会将该key改成永久有效
	 * @param k
	 * @param v
	 * @return
	 */
	public boolean set(final String k, final String v) {  
        if (k == null) {  
            return false;  
        }  
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
            public Boolean doInRedis(RedisConnection connection)  
                    throws DataAccessException {  
                RedisSerializer<String> serializer = getRedisSerializer();  
                byte[] key  = serializer.serialize(k);  
                byte[] name = serializer.serialize(v);  
                connection.set(key, name);  
                return true;  
            }  
        });  
        return result;  
    } 
	
	public boolean set(final String k, final String v, final long validSeconds) {  
        if (k == null) {  
            return false;  
        }  
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
            public Boolean doInRedis(RedisConnection connection)  
                    throws DataAccessException {  
                RedisSerializer<String> serializer = getRedisSerializer();  
                byte[] key  = serializer.serialize(k);  
                byte[] name = serializer.serialize(v);  
                connection.set(key, name);  
                return connection.expire(key, validSeconds);  
            }  
        });  
        return result;  
    } 
	
	public String get(final String keyId) {  
        String result = redisTemplate.execute(new RedisCallback<String>() {  
            public String doInRedis(RedisConnection connection)  
                    throws DataAccessException {  
                RedisSerializer<String> serializer = getRedisSerializer();  
                byte[] key = serializer.serialize(keyId);  
                byte[] value = connection.get(key);  
                if (value == null) {  
                    return null;  
                }  
                String name = serializer.deserialize(value);  
                return name;  
            }  
        });  
        return result;  
    }

	public RedisTemplate<String, String> getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}  
}
